package com.archives.blog.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.archives.blog.common.domain.dao.ArticleChannelRepository;
import com.archives.blog.common.domain.entity.ArticleChannel;
import com.archives.blog.common.domain.pojo.request.ArticleChannelAddRequest;
import com.archives.blog.common.domain.pojo.request.ArticleChannelModifyRequest;
import com.archives.blog.common.enums.ErrorCodeEnum;
import com.archives.blog.common.exp.BlogException;
import com.archives.blog.manager.ArticleChannelManager;

@Service
public class ArticleChannelManagerImpl implements ArticleChannelManager {

	@Autowired
	private ArticleChannelRepository articleChannelRepository;
	
	@Override
	public void add(ArticleChannelAddRequest request) {
		if (request.getParentId() != null) {
			ArticleChannel parent = articleChannelRepository.findOne(request.getParentId());
			if(parent == null){
				throw new BlogException(ErrorCodeEnum.DATA_ILLEGAL, "父级分类不存在");
			}
		}
		ArticleChannel channel = new ArticleChannel();
		channel.create(request);
		articleChannelRepository.save(channel);
	}

	@Override
	public void modify(ArticleChannelModifyRequest request) {
		ArticleChannel channel = articleChannelRepository.findOne(request.getChannelId());
		if(channel == null){
			throw new BlogException(ErrorCodeEnum.DATA_ILLEGAL, "分类不存在");
		}
		channel.modify(request);
		articleChannelRepository.save(channel);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<ArticleChannel> getAll() {
		return articleChannelRepository.findAll();
	}


}
